package com.tiku.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tiku.common.ResultJson;
import com.tiku.controller.DTO.ProductSpecificationsDTO;
import com.tiku.entity.ProductSpecifications;
import com.tiku.entity.UserActiveType;
import com.tiku.mapper.ProductSpecificationsMapper;
import com.tiku.service.UserActiveTypeService;
import com.tiku.mapper.UserActiveTypeMapper;
import com.tiku.utils.ThreadLocalUtil;
import org.apache.ibatis.reflection.ArrayUtil;
import org.bouncycastle.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 *
 */
@Service
public class UserActiveTypeServiceImpl extends ServiceImpl<UserActiveTypeMapper, UserActiveType>
    implements UserActiveTypeService{


    @Autowired
    private UserActiveTypeMapper activeTypeMapper;

    @Autowired
    private ProductSpecificationsMapper specificationsMapper;

    @Override
    public ResultJson getUserActiveTypes() {
        String threadLocal = ThreadLocalUtil.getThreadLocal();
        List<ProductSpecificationsDTO> productSpecifications = specificationsMapper.selectCheckedList(threadLocal);
        if (productSpecifications.isEmpty()){
            productSpecifications = specificationsMapper.selectDefutCheckedList();
            productSpecifications.get(0).setChecked(1);

        }

        return ResultJson.success(productSpecifications);

    }

    @Override
    public ResultJson updateActiveTypes(List<String> ids) {

        if (ids==null||ids.isEmpty()){
            return ResultJson.error("选择不能为空");
        }
        String userId = ThreadLocalUtil.getThreadLocal();
        activeTypeMapper.delete(new LambdaQueryWrapper<>(UserActiveType.class).eq(UserActiveType::getUserId,userId));
        List<UserActiveType> userActiveTypes = new ArrayList<>();
        for (String id : ids) {
            UserActiveType userActiveType = new UserActiveType();
            userActiveType.setUserId(userId);
            userActiveType.setTypeClassId(id);
            userActiveTypes.add(userActiveType);
        }
        userActiveTypes.get(0).setChecked(1);

        boolean b = saveBatch(userActiveTypes);
        return b?ResultJson.success("ok"):ResultJson.sysError();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultJson activeType(String id) {
        String userId = ThreadLocalUtil.getThreadLocal();
        boolean update1 = update(new LambdaUpdateWrapper<>(UserActiveType.class).eq(UserActiveType::getUserId, userId)
                .eq(UserActiveType::getChecked, 1).set(UserActiveType::getChecked, 0));

        boolean update = update(new LambdaUpdateWrapper<>(UserActiveType.class).eq(UserActiveType::getUserId, userId)
                .eq(UserActiveType::getTypeClassId, id).set(UserActiveType::getChecked, 1));

        return update?ResultJson.success("ok"):ResultJson.sysError();
    }
}




